﻿/* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1
 *
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations
 * under the License.
 *
 * The Original Code is tyzoh.jp code.
 *
 * The Initial Developer of the Original Code is Kazutaka Kurihara.
 * Portions created by the Initial Developer are Copyright (C) 2007
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *
 * ***** END LICENSE BLOCK ***** */
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.IO.Compression;

namespace Qurihara.QBasic
{
    public class ZipUtil
    {

        //public static void Zip(string[] srcFiles, string baseDir, string outFile)
        //{
        //    java.io.FileOutputStream fos = new java.io.FileOutputStream(outFile);
        //    java.util.zip.ZipOutputStream zos =
        //        new java.util.zip.ZipOutputStream(fos);

        //    foreach (string file in srcFiles)
        //    {
        //        //string f = System.IO.Path.GetFileName(file);
        //        //相対パスにする file にbaseDirが含まれていることを仮定
        //        string f = System.IO.Path.GetFullPath(file);
        //        string d = System.IO.Path.GetFullPath(baseDir);
        //        if (f.StartsWith(d)) f = f.Replace(d, "");
        //        //ディレクトリを保持する
        //        f = f.Remove(
        //            0, System.IO.Path.GetPathRoot(f).Length);
        //        f = f.Replace("\\", "/");


        //        java.util.zip.ZipEntry ze = new java.util.zip.ZipEntry(f);
        //        ze.setMethod(java.util.zip.ZipEntry.DEFLATED);
        //        zos.putNextEntry(ze);
        //        java.io.FileInputStream fis = new java.io.FileInputStream(file);
        //        sbyte[] buffer = new sbyte[8192];
        //        int len;
        //        while ((len = fis.read(buffer, 0, buffer.Length)) > 0)
        //        {
        //            zos.write(buffer, 0, len);
        //        }
        //        fis.close();
        //        zos.closeEntry();
        //    }
        //    zos.close();
        //    fos.close();
        //}

        //public static void Unzip(string srcZipPath, string extDir)
        //{
        //    java.io.FileInputStream fis = new java.io.FileInputStream(srcZipPath);
        //    java.util.zip.ZipInputStream zis =
        //        new java.util.zip.ZipInputStream(fis);
        //    java.util.zip.ZipEntry ze;
        //    while ((ze = zis.getNextEntry()) != null)
        //    {
        //        if (!ze.isDirectory())
        //        {
        //            string fileName = System.IO.Path.GetFileName(ze.getName());
        //            string destDir = System.IO.Path.Combine(
        //                extDir, System.IO.Path.GetDirectoryName(ze.getName()));
        //            System.IO.Directory.CreateDirectory(destDir);
        //            string destPath = System.IO.Path.Combine(destDir, fileName);
        //            java.io.FileOutputStream fos =
        //                new java.io.FileOutputStream(destPath);
        //            sbyte[] buffer = new sbyte[8192];
        //            int len;
        //            while ((len = zis.read(buffer, 0, buffer.Length)) > 0)
        //            {
        //                fos.write(buffer, 0, len);
        //            }
        //            fos.close();
        //        }
        //    }
        //    zis.close();
        //    fis.close();
        //}

        //public static void DumpZip(string srcZipPath)
        //{
        //    java.io.FileInputStream fis =
        //        new java.io.FileInputStream(srcZipPath);
        //    java.util.zip.ZipInputStream zis =
        //        new java.util.zip.ZipInputStream(fis);

        //    java.util.zip.ZipEntry ze;
        //    while ((ze = zis.getNextEntry()) != null)
        //    {
        //        if (!ze.isDirectory())
        //        {
        //            Console.WriteLine("ファイル名 : {0}", ze.getName());
        //            Console.WriteLine("サイズ : {0} bytes", ze.getSize());
        //            Console.WriteLine("圧縮サイズ : {0} bytes",
        //                ze.getCompressedSize());
        //            Console.WriteLine("CRC : {0:X}", ze.getCrc());
        //            java.util.Date date = new java.util.Date(ze.getTime());
        //            Console.WriteLine("日時 : {0}", date.ToString());
        //            Console.WriteLine();
        //        }
        //    }
        //    zis.close();
        //    fis.close();
        //}

        public static void GZip(string srcPath)
        {
            string outPath = Path.GetFileName(srcPath) + ".gz";

            FileStream inStream
              = new FileStream(srcPath, FileMode.Open, FileAccess.Read);

            FileStream outStream
              = new FileStream(outPath, FileMode.Create);

            using (inStream)
            using (outStream)
            {
                GZip(inStream, outStream);
            }
        }
        public static void GZip(Stream inStream, Stream outStream)
        {
            int num;
            byte[] buf = new byte[1024];

            GZipStream compStream
                = new GZipStream(
                    outStream,
                    CompressionMode.Compress);

            using (compStream)
            {
                while ((num = inStream.Read(buf, 0, buf.Length)) > 0)
                {
                    compStream.Write(buf, 0, num);
                }
            }
        }
        public static void GUnZip(string srcPath)
        {
             if (!srcPath.ToLower().EndsWith(".gz")) {
                return;
             }

            string outPath = srcPath.Substring(0, srcPath.Length - 3);

            FileStream inStream 
            = new FileStream(srcPath, FileMode.Open, FileAccess.Read);

            FileStream outStream
            = new FileStream(outPath, FileMode.Create);

            using (inStream)
            using (outStream)
            {
                GUnZip(inStream, outStream);
            }
        }
        public static void GUnZip(Stream inStream, Stream outStream)
        {
            int num;
            byte[] buf = new byte[1024];
            GZipStream decompStream
              = new GZipStream(
                inStream,
                CompressionMode.Decompress);

            using (decompStream)
            {
                while ((num = decompStream.Read(buf, 0, buf.Length)) > 0)
                {
                    outStream.Write(buf, 0, num);
                }
            }
        }
    }
}
